/* ----- clock ----- */

#include <dos.h>
#include <stdio.h>
#include <conio.h
#include <bios.h>
#include <time.h>
#include "tsr.h"
#define ROW 5
#define COL 30
#define WID 24
#define HIGH 1
#define vad(x,y) ((y)*160+(x)*2)

/* --------- modify for the specific TSR ----------- */
unsigned scancode = 52;   /* . */       /* hot key scan code */
unsigned keymask = 8;     /* Alt */     /* hot key status mask */
char signature[]="CLOCK";               /* tsr signature */

static int VSG;

void Msg_start(void);
void Msg_resid(void);
void Msg_restr(void);
static void Msg_instr(void);

void gettext(int,int,int,int,char *);
void puttext(int,int,int,int,char *);


void popup()
{
   static int c;
   static int sv[WID*HIGH*2];
   static char addr[10];
   static unsigned v,ch,kh,x1;
   static char *tm;
   static long tim,tim2;
   extern int terminating,suspending;
   static int col=COL,row=ROW;
   int done;

   if (peekb(0,0x44a)!=80 || (peekb(0,0x449)!=2 && peekb(0,0x449)!=3))
       return;

   if ((biosequip()&0x30)==0x30) VSG=0XB000;else VSG=0XB800;
   done=0;
   while (!done) {
      gettext(col,row,col+WID-1,row+HIGH-1,sv);
      kh=keyhit();
      while (!kh) {
         time(&tim);
         if (tim!=tim2) {
            tim2=tim;
            tm=ctime(&tim);
            for (v=0;v<WID;v++){
               ch=*(tm+v)|0x7000;
               puttext(col+v,row,col+v,row,&ch);
            }
         }
         kh=keyhit();
      }
      puttext(col,row,col+WID-1,row+HIGH-1,sv);
      tim2=0;
      c=get_char();
      if (c>127) {
         int sh;
         sh=peekb(0,0x417)&0x03;
         c=c-128;
         switch (c){
            case 72: /* up */
               if (row>0) row--;                if (sh) row=0;
               break;
            case 75: /* left */
               if (col>0) col--;                if (sh) col=0;
               break;
            case 80: /* down */
               if (row<25-HIGH) row++;          if (sh) row=25-HIGH;
               break;
            case 77: /* right */
               if (col<80-WID) col++;           if (sh) col=80-WID;
               break;
            default:
               c=1;
               done=1;
         }
      }
      else done=1;
   }
   terminating=(c=='u'||c=='U');
   suspending=(c=='s'||c=='S');
}

void gettext(int cs,int rs,int ce,int re,int *sv)
{
    int *ptr;
    int cp,rp;

    ptr=sv;

    for (cp=cs; cp<=ce; cp++)
        for (rp=rs; rp<=re; rp++) {
           *ptr=vpeek(VSG,vad(cp,rp));ptr++;
        }
}

void puttext(int cs,int rs,int ce,int re,int *sv)
{
    int *ptr;
    int cp,rp;
    int chr,atr;

    ptr=sv;

    for (cp=cs; cp<=ce; cp++)
        for (rp=rs; rp<=re; rp++) {
           vpoke(VSG,vad(cp,rp),*ptr);ptr++;
        }
}


void openfiles(){}
void closefiles(){}

void Msg_start(void)
{
   printf("\nMemory Resident Clock is loaded...\n\n");
   Msg_instr();
}
void Msg_resid(void)
{
   printf("\nMemory Resident Clock is already resident.\n\n");
   Msg_instr();
}
void Msg_restr(void)
{
   printf("\nMemory Resident Clock is restarted.\n\n");
   Msg_instr();
}
void Msg_instr()
{
   printf("  * Invoke the clock at any time with < Alt . >\n\n");
   printf("  * Once invoked, the following apply:\n");
   printf("      U key - unloads the clock\n");
   printf("      S key - suspends the clock\n");
   printf("      Cursor keys & shift key - moves the clock\n");
   printf("            on the screen to where you want it.\n");
   printf("      Any other key puts the clock away.\n\n");
   printf("  * To unsuspend the clock, rerun the \n");
   printf("    disk copy of the program.\n\n");
}
